package org.tgcloud.group.web.controller.manager;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.tgcloud.group.core.BaseLogger;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.core.session.SessionFactory;
import org.tgcloud.group.web.exception.NeedLoginException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 拦截登录授权的校验
 */
@Controller("ManageAPPBaseController")
public class ManageAPPBaseController extends BaseLogger {

    @Autowired
    private SessionFactory sessionFactory;

    private static List<String> authorityUrls = Lists.newArrayList("/user/");

    /*需要登录才可以获取session*/
    @ModelAttribute
    public void getSession(HttpServletRequest request, Model model)
    {
        /*这里对url做过滤，需要登录权限的进入下一步，其它直接设置null即可*/

        /*如果是worker的话，需要过滤下是否有访问权限*/
        String url = request.getRequestURI();
        logger.info(url + ",request-url: " + StringUtil.getIPAddress(request));
        if (checkNeedAuthority(url))
        {
            String token = request.getParameter("token");
            if (Strings.isNullOrEmpty(token))
            {
                throw new NeedLoginException("需要登录");
            }
            else
            {
                Session session = sessionFactory.getSession(token);
                if (session ==null)
                {
                    throw new NeedLoginException("token过期");
                }
                /*权限判断、角色判断*/
//                LogInfo logAccountLogin = accountLoginMapper.selectByToken(token);
//                if (logAccountLogin == null || logAccountLogin.getStatus() != 1)
//                {
//                    sessionFactory.destroySession(token);
//                    throw new NeedLoginException("需要登录");
//                }
//                // 判断用户状态
//                RobotManagerUser user = robotManagerUserMapper.selectByPrimaryKey(session.getUserId());
//                if (user == null || user.getStatus() != 1) {
//                    throw new NeedLoginException("需要登录");
//                }
                model.addAttribute("session", session);
            }
        }

    }

    public boolean checkNeedAuthority(String url)
    {
        for (String u : authorityUrls)
        {
            if (url.contains(u))
            {
                return true;
            }
        }
        return false;
    }

    public static String getRemoteHost(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }

}
